package open.tech.dawn.rest.module.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import open.tech.dawn.jdbc.SuperServiceImpl;
import open.tech.dawn.rest.module.system.entity.ZTreeNode;
import open.tech.dawn.rest.module.system.mapper.SysRoleMapper;
import open.tech.dawn.rest.module.system.model.SysRelation;
import open.tech.dawn.rest.module.system.model.SysRole;
import open.tech.dawn.rest.module.system.query.RoleQuery;
import open.tech.dawn.rest.module.system.service.ISysRelationService;
import open.tech.dawn.rest.module.system.service.ISysRoleService;
import open.tech.dawn.web.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author joss
 * @since 2019-11-07
 */
@Service
public class SysRoleServiceImpl extends SuperServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private ISysRelationService sysRelationService;

    @Override
    @Transactional
    public void setAuthority(Integer roleId, String ids) {

        // 删除该角色所有的权限
        this.baseMapper.deleteRolesById(roleId);

        // 添加新的权限
        for (Long id : Convert.toLongArray(ids.split(","))) {
            SysRelation relation = new SysRelation();
            relation.setRoleid(roleId);
            relation.setMenuid(id);
            sysRelationService.save(relation);
        }
    }

    @Override
    @Transactional
    public void delRoleById(Integer roleId) {
        //删除角色
        this.baseMapper.deleteById(roleId);

        // 删除该角色所有的权限
        this.baseMapper.deleteRolesById(roleId);
    }

    @Override
    public Page<SysRole> selectRoles(Page<SysRole> page, RoleQuery query) {
        List<SysRole> sysRoles = this.baseMapper.selectRoles(page, query);
        page.setRecords(sysRoles);
        return page;
    }

    @Override
    public int deleteRolesById(Integer roleId) {
        return this.baseMapper.deleteRolesById(roleId);
    }

    @Override
    public List<ZTreeNode> roleTreeList() {
        return this.baseMapper.roleTreeList();
    }

    @Override
    public List<ZTreeNode> roleTreeListByRoleId(String[] roleId) {
        return this.baseMapper.roleTreeListByRoleId(roleId);
    }

    @Override
    public String getRoleNameById(Integer id) {
        if (0 != id) {
            SysRole roleObj = getById(id);
            if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
                return roleObj.getName();
            }
        }
        return "";
    }

    @Override
    public String getRoleNameByIds(String ids) {
        if (ToolUtil.isEmpty(ids)) {
            return "";
        }
        Integer[] roles = Convert.toIntArray(ids);
        StringBuilder sb = new StringBuilder();
        for (int role : roles) {
            SysRole roleObj = baseMapper.selectById(role);
            if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
                sb.append(roleObj.getName()).append(",");
            }
        }
        return StrUtil.removeSuffix(sb.toString(), ",");
    }

}
